﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;


namespace CentralOffice.WebClient
{
    public partial class db1 : System.Web.UI.Page
    {
        private const string Select_App = "SELECT top 20 Id, ApplicationId, ApprovedBy, DateApproved, ContentText, Opinion FROM Tasks.Approvals order by ModifiedTime desc";
        private const string Select_Log = "SELECT top 20 LogID, Message, FormattedMessage FROM Admin.Log  ORDER BY Timestamp DESC ";
        private const string Select_Task = "SELECT top 20 Id, Name FROM Tasks.Tasks order by ModifiedTime desc";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text.Length == 0) return;
                if (TextBox1.Text.Trim().ToLower().StartsWith("select") || TextBox1.Text.Trim().ToLower().StartsWith("exec"))
                {
                    GridView2.DataSource = ExecuteDataTable(TextBox1.Text);
                    GridView2.DataBind();
                }
                else
                {
                    //lblError.Text = "影响的记录数为： " + ExecuteNoQuery(TextBox1.Text);
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        private static DataTable ExecuteDataTable(string sql)
        {
            DataTable dt = null;
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(sql);
                DataSet ds = db.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch
            {
                throw;
            }
            finally
            {
            }
            return dt;
        }

        protected void btnExeLog_Click(object sender, EventArgs e)
        {
            DataBound(Select_Log);
        }

        protected void btnExeTask_Click(object sender, EventArgs e)
        {
            DataBound(Select_Task);
        }

        protected void btnExeApp_Click(object sender, EventArgs e)
        {
            DataBound(Select_App);
        }

        private void DataBound(string sql)
        {
            GridView2.DataSource = ExecuteDataTable(sql);
            GridView2.DataBind();
        }
    }
}
